package com.joey.cloud.psn.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.joey.cloud.annotation.dic.JoeEnableDic;
import com.joey.cloud.communal.dto.UserAuthDto;
import com.joey.cloud.communal.form.PageForm;
import com.joey.cloud.communal.utils.BaseController;
import com.joey.cloud.communal.utils.MybatisPlusSearchUtil;
import com.joey.cloud.communal.utils.PageUtil;
import com.joey.cloud.communal.utils.ResponseUtil;
import com.joey.cloud.communal.vo.PageLongVo;
import com.joey.cloud.psn.business.entity.SysPerson;
import com.joey.cloud.psn.business.service.ISysPersonService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author Joey
 * @since 2021-03-19
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/business/person")
@Slf4j
public class SysPersonController extends BaseController<SysPerson> {
    @Autowired
    ISysPersonService sysPersonService;

    @Override
    public IService<SysPerson> getService() {
        return sysPersonService;
    }


    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "测试")
    @PostMapping("/test")
    @ApiImplicitParams({
            @ApiImplicitParam(name="searchJson",value="检索json",dataType = "string", paramType = "query",required = false,defaultValue = "[{\"key\":\"\",\"value\":\"\",\"type\":\"eq\"}]")
    })
    @JoeEnableDic
    public ResponseUtil<PageLongVo<SysPerson>> test(PageForm form, String searchJson){
        try {
            QueryWrapper qw = MybatisPlusSearchUtil.parseWhereSql(searchJson);
            Page<SysPerson> page = getService().page(PageUtil.getPage(form),qw);
            return ResponseUtil.success(PageLongVo.create(page.getPages(),page.getTotal(),page.getRecords()));
        }catch (Exception e){
            log.error("获取列表-带分页出错,searchJson="+searchJson,e);
            return ResponseUtil.error();
        }
    }


    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取用户名称" )
    @GetMapping("/queryNameById")
    @ApiImplicitParam(name="userIds",value="用户ids",dataType = "string", paramType = "query",required = true)
    public ResponseUtil queryNameById(String userIds){
        try {
            String userNames = sysPersonService.queryNameById(userIds);
            return ResponseUtil.success(userNames);
        }catch (Exception e){
            log.error("获取用户名称出错",e);
            return ResponseUtil.error();
        }
    }
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取用户权限信息" )
    @GetMapping("/queryPsnAuthInfo/{psnId}")
    @ApiImplicitParam(name="psnId",value="用户id",dataType = "string", paramType = "path",required = true)
    public ResponseUtil<UserAuthDto> queryPsnAuthInfo(@PathVariable("psnId") Long psnId){
        try {
            UserAuthDto dto = sysPersonService.queryPsnAuthInfo(psnId);
            return ResponseUtil.success(dto);
        }catch (Exception e){
            log.error("获取用户权限信息出错,psnId="+psnId,e);
            return ResponseUtil.error();
        }
    }
}

